﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DgvFunction
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();

            this.dgv_Data.AutoGenerateColumns = false;
        }

        List<DataMonitor> Monitors = new List<DataMonitor>();


        private void btn_Add_Click(object sender, EventArgs e)
        {
            this.dgv_Data.DataSource = null;
            Monitors.Add(new DataMonitor() { InsertTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Radius = 2.0f, Cc = 0.89f, Angel = 0, Gray = 255, Result = true });
            this.dgv_Data.DataSource = Monitors;
            dgv_Data.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dgv_Data.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            int index = dgv_Data.CurrentCell.RowIndex;
            DialogResult result = MessageBox.Show("确定将点位" + index.ToString() + "删除吗", "运行提示");
            if (result == DialogResult.OK)
            {
                //this.dgv_Data.Rows.RemoveAt(index);
                IList list = (IList)dgv_Data.DataSource;
                list.RemoveAt(dgv_Data.CurrentRow.Index);
                dgv_Data.DataSource = null;
                dgv_Data.DataSource = list;
                dgv_Data.RowsDefaultCellStyle.BackColor = Color.Bisque;
                dgv_Data.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            }
        }


        /// <summary>
        /// 在datagridview行绘制后事件中添加行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DgvRowPostPaint(this.dgv_Data, e);
        }

        /// <summary>
        /// datagridview添加行号
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="e"></param>
        private void DgvRowPostPaint(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                //添加行号 
                SolidBrush v_SolidBrush = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor);
                int v_LineNo = e.RowIndex + 1;
                string v_Line = v_LineNo.ToString();
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

                StringFormat sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Center;

                e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth, dgv.RowTemplate.Height), sf);

            }
            catch (Exception ex)
            {
                MessageBox.Show("添加行号时发生错误，错误信息：" + ex.Message, "操作失败");
            }
        }

        private void dgv_Data_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgv_Data.Rows.Count > 0)
            {
                string cellVal = dgv_Data.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                int rowIndex = e.RowIndex;
                int columnIndex = e.ColumnIndex;

                if (cellVal != null && rowIndex >= 0)
                {
                    FrmEdit frmEdit = new FrmEdit(rowIndex, columnIndex, cellVal);
                    // 【方法1】使用委托回调
                    frmEdit.SettingAction += ChangeText;
                    DialogResult dr = frmEdit.ShowDialog();

                    //【方法2】使用窗体返回值
                    //if (dr == DialogResult.OK)
                    //{
                    //    if (frmEdit.SettingVar != null && frmEdit.SettingVar.Length > 0)
                    //    {
                    //        dgv_Data.CurrentCell.Value = frmEdit.SettingVar;
                    //        dgv_Data.Refresh();
                    //    }

                    //}


                }

            }
        }
        private void ChangeText(string str)
        {
            dgv_Data.CurrentCell.Value = str;
        }


        private void btn_Import_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "XLS文件(*.xlsx)|*.xlsx";

            saveFileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmssms");

            saveFileDialog.DefaultExt = ".xlsx";
            saveFileDialog.AddExtension = true;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (NPOIHelper.SaveToExcelNew(saveFileDialog.FileName, this.dgv_Data))
                {
                    MessageBox.Show("数据导出成功", "信息提示");
                }
            }
        }

        private void btn_Export_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "XLS文件(*.xlsx)|*.xlsx";

            ofd.DefaultExt = ".xlsx";
            ofd.AddExtension = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (NPOIHelper.ReadFromExcel(ofd.FileName, this.dgv_Data))
                {
                    MessageBox.Show("数据导入成功", "信息提示");
                }
            }
        }

        #region 鼠标进入和离开，按钮颜色改变
        private void btn_MouseEnter(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.BackColor = Color.CadetBlue;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.BackColor = Color.FromArgb(211, 211, 211);
        }
        #endregion

        private void btn_Update_Click(object sender, EventArgs e)
        {

        }
    }
}

